<template>
  <div>
    <Main v-if="show" :type="'form'">
      <Title :title="title" />
      <el-form
        ref="form"
        :model="form"
        :rules="form"
        label-width="180px"
        class="demos-forms"
      >
        <el-form-item
          label="活动名称"
          prop="title"
          :rules="{
            required: true,
            message: '请输入活动名称',
            trigger: 'blur'
          }"
        >
          <el-input
            v-model="form.title"
            class="form-input"
            size="medium"
            placeholder="请输入活动名称"
            maxlength="10"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item
          label="活动封面"
          prop="cover"
          :rules="{
            required: true,
            message: '请添加活动封面',
            trigger: 'blur'
          }"
        >
          <div class="commodityImage clearfix">
            <div v-if="form.cover" class="icon">
              <img
                :src="$store.state.qiniu + form.cover + $store.state.imgRotate"
              />
            </div>
            <el-button
              type="primary"
              @click="
                showImages = true;
                useType = 'img';
              "
              >选择图片</el-button
            >
          </div>
        </el-form-item>
        <el-form-item
          label="活动说明"
          prop="introduce"
          :rules="{
            required: true,
            message: '请添加活动说明',
            trigger: 'blur'
          }"
        >
          <el-input
            v-model="form.introduce"
            type="textarea"
            :autosize="{ minRows: 6, maxRows: 8 }"
            placeholder="请输入内容"
            style="width:50%;"
            size="medium"
            maxlength="100"
            show-word-limit
          ></el-input
          ><span style="color:#909399;font-size:12px;">
            可输入100字，不填则不显示</span
          >
        </el-form-item>
        <el-form-item
          label="活动时间"
          prop="time"
          :rules="{
            required: true,
            message: '请输入活动时间',
            trigger: 'blur'
          }"
        >
          <el-date-picker
            v-model="form.time"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="砍价有效期"
          prop="validity"
          :rules="{
            required: true,
            message: '请添砍价有效期',
            trigger: 'blur'
          }"
        >
          <el-input-number
            v-model="form.validity"
            :min="1"
            :controls="false"
            size="medium"
            class="form-input"
            :disabled="form.bargain_id !== ''"
          ></el-input-number>
          &nbsp;&nbsp;小时
        </el-form-item>
        <el-form-item label="砍价模式" :rules="{ required: true }">
          <el-radio-group
            v-model="form.type"
            :disabled="form.bargain_id !== ''"
            @change="typeTag"
          >
            <el-radio label="zero">砍到0元才可购买</el-radio>
            <el-radio label="low">砍到低价才可购买</el-radio>
          </el-radio-group>
          &nbsp;&nbsp;
          <span v-if="form.type == 'low'">
            <el-input-number
              v-model="form.moeny"
              :disabled="form.bargain_id !== ''"
              :min="1"
              :controls="false"
              size="medium"
              style="width:100px;"
            ></el-input-number>
            &nbsp;元&nbsp;&nbsp;&nbsp;&nbsp;
            <span class="form-info">砍价底价默认为0，底价需低于原价</span>
          </span>
        </el-form-item>
        <el-form-item
          label="发起者砍第1刀金额设置"
          prop="one_tage"
          :rules="{
            required: true,
            message: '请输入成团有效时间',
            trigger: 'blur'
          }"
        >
          <el-radio-group
            v-model="form.one_tage"
            :disabled="form.bargain_id !== ''"
          >
            <el-radio :label="50">总额*50%</el-radio>
            <el-radio :label="60">总额*60%</el-radio>
            <el-radio :label="70">总额*70%</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="帮砍金额" :rules="{ required: true }">
          <el-radio-group
            v-model="form.bargain_money"
            :disabled="form.bargain_id !== ''"
          >
            <el-radio label="1">随机金额</el-radio>
            <el-radio label="2">固定金额</el-radio>
          </el-radio-group>

          <span class="form-info">（每位用户帮砍金额一致）</span>
        </el-form-item>

        <el-form-item
          label="帮砍人数"
          prop="people_sum"
          :rules="{
            required: true,
            message: '请输入帮砍人数',
            trigger: 'blur'
          }"
        >
          <el-input-number
            v-model="form.people_sum"
            :disabled="form.bargain_id !== ''"
            :min="2"
            :max="100"
            :controls="false"
            size="medium"
            class="form-input"
          ></el-input-number>
          <span class="form-info"
            >&nbsp;&nbsp;&nbsp;帮砍人数：2~100
            人，每个砍价订单的帮砍人数，达到该人数才可砍至底价、每人只可砍1次</span
          >
        </el-form-item>

        <el-form-item label="好友砍价获劵">
          <el-checkbox v-model="form.checked" @change="checkboxTag"
            >获得优惠券</el-checkbox
          >
          <span class="form-info"
            >&nbsp;&nbsp;&nbsp;向砍价好友赠送优惠券，利于砍价好友在店铺内下单，提高交易量，建议合理添加。</span
          >
          <div v-if="form.checked">
            <span v-if="form.coupon_name"
              >{{ form.coupon_name }}&nbsp;&nbsp;&nbsp;</span
            >
            <el-button type="primary" @click="visible = true"
              >选择优惠劵</el-button
            >
          </div>
        </el-form-item>
      </el-form>

      <Footer>
        <el-button plain @click="$emit('out')">取 消</el-button>
        <el-button
          v-if="title == '新建砍价'"
          type="primary"
          @click="submitForm('add')"
          >保存并添加商品</el-button
        >
        <el-button v-else type="primary" @click="submitForm('edit')"
          >保存并编辑商品</el-button
        >
      </Footer>
      <images
        :dialog-images="showImages"
        :use-btn="true"
        :use-type="'img'"
        @close="close"
      />
    </Main>
    <shop v-else :assemble-id="assembleId" :types="types" @out="$emit('out')" />
    <coupon :visible="visible" :val="form.coupon_id" @outDialog="outDialog" />
  </div>
</template>
<script>
import { ADD_BARGAIN } from "../service";

export default {
  components: {
    images: r =>
      require.ensure([], () => r(require("publicCom/imgupdate")), "imgupdate"),
    shop: () => import("./component/shop"),
    coupon: () => import("./component/coupon")
  },
  props: {
    dataVal: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      title: "新建砍价",
      showImages: false,
      form: {
        bargain_id: "",
        title: "",
        cover: "",
        introduce: "",
        time: [],
        validity: "",
        type: "zero",
        moeny: 0,
        bargain_money: "1",
        people_sum: "",
        one_tage: 50,
        checked: false,
        coupon_id: 0,
        coupon_name: ""
      },
      show: true,
      assembleId: "",
      assemble_idE: "",
      types: "",
      slof: "",
      visible: false
    };
  },
  created() {
    if (JSON.stringify(this.dataVal) !== "{}") {
      this.title = "编辑砍价";
      this.form = {
        bargain_id: this.dataVal.bargain_id,
        title: this.dataVal.title,
        cover: this.dataVal.cover,
        introduce: this.dataVal.introduce,
        time: [this.dataVal.stime, this.dataVal.etime],
        validity: this.dataVal.validity,
        type: this.dataVal.type,
        moeny: this.dataVal.moeny,
        bargain_money: this.dataVal.bargain_money,
        people_sum: this.dataVal.people_sum,
        one_tage: this.dataVal.one_tage,
        coupon_id: this.dataVal.lg_coupon_id,
        coupon_name: this.dataVal.coupon_name
      };
      this.form.checked = this.dataVal.lg_coupon_id !== 0;
    } else {
      this.title = "新建砍价";
    }
  },

  methods: {
    outDialog(v) {
      if (v) {
        this.form.coupon_id = v.lg_coupon_id;
        this.form.coupon_name = v.title;
      }
      this.visible = false;
    },
    close(file) {
      this.showImages = false;
      if (!file) return;
      this.form.cover = file.file_path;
    },
    typeTag(e) {
      if (e === "low") this.form.moeny = 0;
    },
    checkboxTag(e) {
      if (!e) this.form.coupon_id = 0;
    },
    async submitForm(type) {
      let prams = {
        bargain_id: this.form.bargain_id,
        title: this.form.title,
        cover: this.form.cover,
        introduce: this.form.introduce,
        stime: this.form.time[0],
        etime: this.form.time[1],
        validity: this.form.validity,
        type: this.form.type,
        moeny: this.form.moeny,
        bargain_money: this.form.bargain_money,
        people_sum: this.form.people_sum,
        one_tage: this.form.one_tage,
        coupon_id: this.form.coupon_id
      };
      await this.$refs["form"].validate(valid => {
        if (valid) {
          ADD_BARGAIN(prams).then(({ code, data, msg }) => {
            if (code === 200) {
              this.show = false;
              this.types = type;
              this.assembleId = data;
            }
            this.$message({
              message: msg,
              type: code === 200 ? "success" : "error"
            });
          });
        } else {
          return false;
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.moblieBoxs {
  width: 378px;
  border: 1px solid #f5f5f5;
  height: 670px;
  background: #f5f5f5;
}
.icon {
  width: 100px;
  height: 100px;
  border-radius: 6px;
  display: block;
  float: left;
  margin-right: 10px;
  overflow: hidden;
  border: 1px solid #ccc;
  background-color: #fff;
  position: relative;
  img {
    width: 100px;
    height: 100px;
  }
}
</style>
